<?php
include('../Connections/KRAPPERCON.php');
include ("../Connections/Accesos.php");

$combo = '';
include ('../Query_Min/getAlmacenes.php');
?>
<div>
<input type="button" value="Nuevo" onClick="newMigo()" >
<input type="button" value="Generar Movimiento" onclick="doMigo()" />
<input type="button" value="Imprimir" onclick="printMigo()" class="noView" /><input name="t_mg_mov" type="text" onkeypress="introNroMov(this,event)" placeholder ='Nro.Movimiento' id="t_mg_mov" />&nbsp;<input name="t_mg_ref" type="text" onkeypress="introRefMov(this,event)" placeholder ='Referencia' id="t_mg_ref" />
</div>
</div>
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tbl_dat_mov">
  <tr>
    <td>Nro.de Movimiento :</td>
    <td><a href="#" id="a_nroMov" onclick="showDetMov(this)"></a></td>
  </tr>
  <tr>
    <td>Clase de Movimiento :</td>
    <td>
      <select name="cbo_tr_cldoc" id="cbo_tr_cldoc" onchange="fieldsMigo(this)">
      <?php 
	  $cladoc = 'MV';
		include('../Query_Min/getClaDocs.php');
	  ?>
      </select>
    </td>
  </tr>
  <!-- 
  <tr>
    <td>Referencia :</td>
    <td>
      <input type="text" name="t_tr_ref" id="t_tr_ref">
    </td>
  </tr>
  -->
  <tr>
    <td>Texto :</td>
    <td>
      <input name="t_tr_txt" class="clsReq" type="text" id="t_tr_txt" size="50" maxlength="45">
    </td>
  </tr>
  <tr id="trOrig">
    <td>Origen :</td>
    <td>
      <select name="cbo_tr_ori" id="cbo_tr_ori">
      <?php echo $combo;?>
      </select>
    </td>
  </tr>
  <tr id="trDest">
    <td>Destino :</td>
    <td>
      <select name="cbo_tr_des" id="cbo_tr_des">
      <?php echo $combo;?>      
      </select>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="2">
    <table width="100%" border="1" cellspacing="0" id="tbl_tra_itm" cellpadding="0" class="tableIn" rules="all">
    <thead>
      <tr>
        <th>Articulo</th>
        <th>Descripcion</th>
        <th>Stk.Libre</th>
        <th>Cantidad</th>        
        <th>Costo UN</th>
        <th>&nbsp;</th>
        </tr>
      </thead>
      <tbody>
      <?php 
	  for($a = 0;$a<10;$a++){
	  ?>
      <tr class="<?php if ($a==9) { echo 'trLast'; }?>">
        <td>
          <input class="clsTraCod" type="text" ondblclick="matchArt(this);" name="t_tr_cod" id="t_tr_cod" onchange="getArtTrans(this)" onkeypress="introArtTrans(this,event)"></td>
        <td class="clsTraTxt">&nbsp;</td>
        <td class="clsTraStk qt" style="font-weight:bold;">&nbsp;</td>
        <td>
          <input type="text" onchange="mr(this);" class="clsTraQty qt" name="t_tr_ctd" id="t_tr_ctd" ></td>
        <td class="clsTraCos qt">
          <!--<input type="text" name="t_tr_cos" id="t_tr_cos" class="" onchange="mr(this)" disabled="disabled"  />-->
        </td>
        <td>
          <input type="button" name="button" id="button" onclick="delPosMigo(this)" value="X"></td>
        </tr>
        <?php 
		}
		?>
        </tbody>
        <tfoot>
        <tr>
         <td><input name="btn_addnew" type="button" value="Agregar" onclick="addPosTR()" /></td>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
         <td>&nbsp;</td>
         <td></td>
         <td></td>
        </tr>
        </tfoot>
    </table></td>
  </tr>
</table>
<script type="text/javascript">
GEBI('cbo_tr_cldoc').value = 'MV_TR';
function addPosTR(){
var row = $('#tbl_tra_itm tbody .trLast').clone().removeClass('trLast').css('display','');
	$('#tbl_tra_itm tbody').append(row);
}

function introArtTrans(obj,e){
	if (e.keyCode == 13){
		getArtTrans(obj);
	}
}

function delPosMigo(obj){
	var fila = $(obj).parent().parent();
	fila.remove();
}

function getArtTrans(obj){
	obj = $(obj);
	obj.txtLoad();
	var fila = obj.parent().parent();
	var alm = '';
	var cldoc = GEBI('cbo_tr_cldoc').value;
	//alert(obj.value);
	if(cldoc == 'MV_TR'){
	alm = GEBI('cbo_tr_ori');
	}
	else if (cldoc == 'MV_EM') {
	alm = GEBI('cbo_tr_des');
	}
	else if (cldoc == 'MV_SM'){
	alm = GEBI('cbo_tr_ori');
	}

	sendData('Query_Min/getDatArticulo.php',{ field : 'venta' , oper : 'view' , trans : cldoc , t_codart : obj.val(), t_codalm : alm.value}, function(){
		var objx = eval('(' + $("#d_result").html() + ')');
		//fila.find('.clsTraCod').html(objx.code);
		obj.val(objx.code);
		obj.etxtLoad();
		fila.find('.clsTraTxt').html(objx.desc);
		fila.find('.clsTraStk').html(myRound(objx.stklib));
		fila.find('.clsTraCos').html(pFloat(objx.costo));
	});
}

function newMigo(){
loadPage('Procesos/MantMovimientos.php');
}

function doMigo(){

	if (!$("#tbl_dat_mov").checkInput()){
	return;
	}

var itms = '';
$('#tbl_tra_itm tbody tr').each(function(){
	var fila = $(this);
	var cod = fila.find('.clsTraCod').val();
	if ($.trim(cod)!='') {
	var qty = pInt(fila.find('.clsTraQty').val());
	//var cst = pFloat(fila.find('.clsTraCos').val());
	itms = itms + cod + '|' + qty  +  '#';
	}
	else {
	//fila.remove();
	}
});

sendData('Procesos/Opers/OperMigo.php',
	{ oper : 'upd' ,
//	 check : 'X',
	 cbo_tr_cldoc : GEBI('cbo_tr_cldoc').value,
	 t_tr_ref : GEBI('t_tr_ref').value,
	 t_tr_txt : GEBI('t_tr_txt').value,
	 cbo_tr_ori : GEBI('cbo_tr_ori').value,
	 cbo_tr_des : GEBI('cbo_tr_des').value,
	 items :	itms
	 }
	);
	
}

function fieldsMigo(obj){
	if(obj.value == 'MV_TR'){
		$('.clsTraCos').disable();
		$('#trOrig').show();
		$('#trDest').show();
	}
	else if (obj.value == 'MV_EM') {
		$('.clsTraCos').enable();
		$('#trOrig').hide();
		$('#trDest').show();
	}
	else if (obj.value == 'MV_SM'){
		$('.clsTraCos').disable();
		$('#trOrig').show();
		$('#trDest').hide();
	}
}

function printMigo(){
	
}

function showDetMov(obj){
/**
$codart = $_POST['t_kx_codart'];
$alm = $_POST['cbo_kx_alm'];
$fini = $_POST['t_kx_fini'];
$ffin = $_POST['t_kx_ffin'];
$clase = $_POST['cbo_kx_clase'];
**/
var mov = obj.innerHTML;
loadPopup(
	{
url : 'Consultas/Opers/queryKardex.php',
params : 
		{
		t_kx_codart : '',
		cbo_kx_alm : '',
		t_kx_fini : '',
		t_kx_ffin : '',
		cbo_kx_clase : 4,
		movimiento : mov
		}
	}
	);
}

function introNroMov(obj,e){
	if (e.keyCode==13){
	var mov = obj.value;
	GEBI('t_mg_ref').value = '';
	loadPopup(
	{
	url : 'Consultas/Opers/queryKardex.php',
	params : 
		{
		t_kx_codart : '',
		cbo_kx_alm : '',
		t_kx_fini : '',
		t_kx_ffin : '',
		cbo_kx_clase : '',
		movimiento : mov
		}
	}
	);
	}
}

function introRefMov(obj,e){
	if (e.keyCode==13){
	GEBI('t_mg_mov').value = '';
	var ref = obj.value;
	loadPopup(
	{
	url : 'Consultas/Opers/queryKardex.php',
	params : 
		{
		t_kx_codart : '',
		cbo_kx_alm : '',
		t_kx_fini : '',
		t_kx_ffin : '',
		cbo_kx_clase : '',
		movimiento : '',
		referencia : ref
		}
	}
	);
	}
}



</script>